package LeetCode;

import java.util.Scanner;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/4/23.
// 
public class 字符串转换整数 {
    //脑瘫题目，无需理会
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println(myAtoi(scan.nextLine()));
    }
    public static  int myAtoi(String s) {
        long num = 0;
        s = s.replaceAll(" ","");
        int start,end;
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == '+' || s.charAt(i) == '-' || ('0' < s.charAt(i) && '9' > s.charAt(i))){
                start = i;
                end = i + 1;
                while(end < s.length() && ('0' <= s.charAt(end) && '9' >= s.charAt(end))) {
                    end++;
                }
                s = s.substring(start,end);
                break;
            }
        }
        if(s.charAt(0) == '-' || s.charAt(0) == '+'){
            num = convertNumber(s,1);
        }else {
            num = convertNumber(s,0);
        }
        if(s.charAt(0) == '-'){
            return (int) (-1 * num);
        }else {
            if(num > Math.pow(2,31)-1){
                return (int) (Math.pow(2,31)-1);
            }else {
                return (int) num;
            }

        }
    }
    public static long convertNumber(String s,int start){
        long num = 0;
        for (int i = start; i < s.length(); i++) {
            num += Character.getNumericValue(s.charAt(i));
            if(num > (long) (Math.pow(2,31))){
                num =(long) Math.pow(2,31);
                break;
            }else {
                if(i + 1 < s.length()){
                    num *= 10;
                }
            }
        }
        return num;
    }
}
